<?php

class BlogController extends Zend_Controller_Action 
{

	public function init()
	{
		$this->view->headScript()->appendFile('/public/tiny_mce/tiny_mce.js', 'text/javascript');
		$this->view->headScript()->appendFile('/public/tiny_mce/tiny_mce-init.js', 'text/javascript');
	}
	
	public function indexAction()
	{
		// @todo Add Blog listing on index action
		//$blog = new Application_Model_BlogMapper();
		//$this->view->entries = $blog->fetchAll();
		
		$blog = new Application_Model_Blog();
		
		if( function_exists("gd_info") ) {
		  echo 'yes';
		} else {
		  echo 'no';
		}
		
		// show latest entries first
		$blog_entries = $blog->fetchAll(null, 'BLEN_CREATE_DATE desc');
		
		$this->view->entries = $blog_entries;
	}
	
	public function detailsAction($id)
	{
		
	}
	
	public function addAction()
	{
		$request = $this->getRequest();
		$form = new Application_Form_Blog(); 
		
		if ($this->getRequest()->isPost())
		{
			if ($form->isValid($request->getPost()))
			{
				$blog = new Application_Model_Blog($form->getValues());
				
				// @todo Update to reflect auth user and status dropdown on form
				$data = array( 
					'BLEN_TITLE'		=> $form->getValue('BLEN_TITLE'), 
					'BLEN_DESC' 		=> $form->getValue('BLEN_DESC'), 
					'BLEN_CREATE_DATE'	=> new Zend_Db_Expr('GetUTCDate()'), 
					'BLEN_CREATE_USER'	=> 'DA94E763-A8BC-4288-99B2-9A30D0DC2786', 
					'BLST_ID'			=> $form->getValue('BLST_ID')
				);
				
				$blog->insert($data);
				
				return $this->_helper->redirector('index');
			}
		}
		
		// get a list of statuses 
		$status = new Application_Model_BlogStatus();
		$status = $status->fetchAll();
		$selectList = $form->getElement('BLST_ID');
		
		foreach ($status as $row)
		{
			$selectList->addMultiOption($row->BLST_ID, $row->BLST_DESC);
		}
		
		$this->view->form = $form;
		
	}
	
	public function editAction()
	{
		
		$request = $this->getRequest();
		$form = new Application_Form_Blog();
		
		
		if ($this->getRequest()->isPost())
		{
			// Zend Hash isnt working so implement my own
			$mysession = new Zend_Session_Namespace('csrf');
			$hash = $mysession->csrf;
			if (!$hash == $form->getValue('csrf'))
			{
				throw new Exception('Could not verify hash token');
			}
			
			if ($form->isValid($request->getPost()) && 
					$form->getValue('BLEN_ID') == $this->_getParam("id"))
			{
				$blog = new Application_Model_Blog($form->getValues());
				
				var_dump($form);
				
				$data = array( 
					'BLEN_TITLE'		=> $form->getValue('BLEN_TITLE'), 
					'BLEN_DESC' 		=> $form->getValue('BLEN_DESC')
				);
				
				$blog->update($data, array('BLEN_ID = ?' => $form->getValue('BLEN_ID')));
				
				//echo $form->getValue('csrf');
				return $this->_helper->redirector('index');
			}
		}
		
		//$hash = new Zend_Form_Element_Hash('csrf');
		//$form->addElement('hash', 'csrf', array('salt' => 'unique'));
		
		$blog = new Application_Model_Blog();
		$id = $this->_getParam("id");
		$blog = $blog->find($id);
		
		if ($blog->count() == 0 || $blog->count() > 1)
		{
			throw new Exception('Edit action found more than 1 row with PK: ' . $id);
		}
		
		// get a list of statuses 
		$status = new Application_Model_BlogStatus();
		$status = $status->fetchAll();
		$selectList = $form->getElement('BLST_ID');
		
		foreach ($status as $row)
		{
			$selectList->addMultiOption($row->BLST_ID, $row->BLST_DESC);
		}
		
		$blog = $blog->current();
		
		$data = array(
			'BLEN_TITLE' 		=> $blog->BLEN_TITLE, 
			'BLEN_DESC'			=> $blog->BLEN_DESC, 
			'BLEN_ID'			=> $blog->BLEN_ID, 
			'BLST_ID'			=> $blog->BLST_ID
		);
		
		$form->populate($data);
		
		$this->view->form = $form;
		
		/*
		if (!$this->getRequest()->isPost())
		{
		
			$id = $this->_getParam("id");
			$blog = new Application_Model_Blog();
			
			// get the rowset object for this id
			$blog = $blog->find($id);
			
			// make sure only 1 row was returned
			if ($blog->count() == 0 || $blog->count() > 1)
			{
				throw new Exception('Edit Action found more than 1 row with PK: ' . $id);
			}
			
			// get the row object
			$blog = $blog->current();
			
			$this->view->entry = $blog;
		}
		else 
		{
			// form was submitted, validate and verify
			$post = $this->getRequest()->getPost();
			
			$form = new Zend_Form();
			
			if ($form->isValid($post))
			{
				if (strlen($post['BLEN_TITLE']) > 100 || strlen($post['BLEN_TITLE']) == 0)
				{
					throw new Exception('Title must not be empty and contain no more than 100 characters');
				}
				
				if (strlen($post['BLEN_DESC']) > 2000 || strlen($post['BLEN_DESC']) == 0)
				{
					throw new Exception('Description must not be empty and contain no more than 2000 characters');
				}
				
				echo 'past exceptions';
			}
			else
			{
				throw new Exception('Invalid form submission');
			}
			
			//var_dump($this->getRequest()->getPost());
			//echo "testing!!!";	
		}
		*/
		
		
	}
	
}